package com.qdxwx.data.service.user;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKClientOs;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.mapper.user.UserLoginMapper;
import com.qdxwx.models.user.User;
import com.qdxwx.models.user.UserLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {

	@Autowired
	private UserLoginMapper userLoginMapper;

	@Override
	public int insert(UserLogin loginLog) {
		loginLog.setLoginTime(LocalDateTime.now());
		return userLoginMapper.createLoginLog(loginLog);
	}

	@Override
	public int create(HttpServletRequest request, User user) {
		//插入登录日志
		UserLogin loginLog = new UserLogin();
		loginLog.setUserIp(TKRequest.getIpAddr(request));
		loginLog.setUserOs(TKClientOs.parse(request).getName());
		loginLog.setUserAgent(request.getHeader("User-Agent"));
		loginLog.setUserId(user.getUserId());
		loginLog.setUserName(user.getDisplayName());
		return insert(loginLog);
	}

	public List<UserLogin> getList(long userId, PageRequest page) {
		return userLoginMapper.selectList(userId, page);
	}

	@Override
	public long count(long userId) {
		return userLoginMapper.selectCount(userId);
	}

}
